<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="分配单号" prop="allocationNo">
        <el-input v-model="queryParams.allocationNo" placeholder="请输入分配单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="出库单号" prop="outboundOrderNo">
        <el-input v-model="queryParams.outboundOrderNo" placeholder="请输入出库单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="分配状态" prop="allocationStatus">
        <el-select v-model="queryParams.allocationStatus" placeholder="请选择状态" clearable>
          <el-option label="待分配" value="pending" />
          <el-option label="分配中" value="in_progress" />
          <el-option label="已完成" value="completed" />
          <el-option label="已取消" value="cancelled" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="allocationList" border stripe>
      <el-table-column label="分配单号" align="center" prop="allocationNo" width="160" />
      <el-table-column label="出库单号" align="center" prop="outboundOrderNo" width="160" />
      <el-table-column label="分配策略" align="center" prop="allocationStrategy" width="120">
        <template slot-scope="scope">
          {{ getStrategyText(scope.row.allocationStrategy) }}
        </template>
      </el-table-column>
      <el-table-column label="分配状态" align="center" prop="allocationStatus" width="100">
        <template slot-scope="scope">
          <el-tag :type="getStatusTag(scope.row.allocationStatus)" size="small">
            {{ getStatusText(scope.row.allocationStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="总行数" align="center" prop="totalLines" width="80" />
      <el-table-column label="已分配行数" align="center" prop="allocatedLines" width="100" />
      <el-table-column label="总数量" align="center" prop="totalQuantity" width="100" />
      <el-table-column label="已分配数量" align="center" prop="allocatedQuantity" width="100" />
      <el-table-column label="分配人" align="center" prop="allocator" width="100" />
      <el-table-column label="分配时间" align="center" prop="allocationTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)" v-hasPermi="['wms:allocation:query']">详情</el-button>
          <el-button v-if="scope.row.allocationStatus === 'completed'" size="mini" type="text" icon="el-icon-close" @click="handleCancel(scope.row)" v-hasPermi="['wms:allocation:cancel']">取消</el-button>
          <el-button v-if="scope.row.allocationStatus === 'pending'" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['wms:allocation:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 详情对话框 -->
    <el-dialog title="库存分配详情" :visible.sync="detailOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="分配单号">{{ detailData.allocationNo }}</el-descriptions-item>
        <el-descriptions-item label="出库单号">{{ detailData.outboundOrderNo }}</el-descriptions-item>
        <el-descriptions-item label="分配策略">{{ getStrategyText(detailData.allocationStrategy) }}</el-descriptions-item>
        <el-descriptions-item label="分配状态">
          <el-tag :type="getStatusTag(detailData.allocationStatus)" size="small">
            {{ getStatusText(detailData.allocationStatus) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="总行数">{{ detailData.totalLines }}</el-descriptions-item>
        <el-descriptions-item label="已分配行数">{{ detailData.allocatedLines }}</el-descriptions-item>
        <el-descriptions-item label="总数量">{{ detailData.totalQuantity }}</el-descriptions-item>
        <el-descriptions-item label="已分配数量">{{ detailData.allocatedQuantity }}</el-descriptions-item>
        <el-descriptions-item label="分配人">{{ detailData.allocator }}</el-descriptions-item>
        <el-descriptions-item label="分配时间">{{ detailData.allocationTime }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ detailData.remark }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAllocation, getAllocation, cancelAllocation, delAllocation } from '@/api/wms/allocation'

export default {
  name: 'WmsAllocation',
  data() {
    return {
      loading: true,
      showSearch: true,
      total: 0,
      allocationList: [],
      detailOpen: false,
      detailData: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        allocationNo: null,
        outboundOrderNo: null,
        allocationStatus: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listAllocation(this.queryParams).then(response => {
        this.allocationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleView(row) {
      getAllocation(row.id).then(response => {
        this.detailData = response.data
        this.detailOpen = true
      })
    },
    handleCancel(row) {
      this.$prompt('请输入取消原因', '取消分配', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /.+/,
        inputErrorMessage: '取消原因不能为空'
      }).then(({ value }) => {
        cancelAllocation(row.id, value).then(() => {
          this.$message.success('取消成功')
          this.getList()
        })
      })
    },
    handleDelete(row) {
      this.$confirm('确认删除该分配记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delAllocation(row.id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        })
      })
    },
    getStatusTag(status) {
      const map = { 'pending': 'info', 'in_progress': 'warning', 'completed': 'success', 'cancelled': 'danger', 'failed': 'danger' }
      return map[status] || 'info'
    },
    getStatusText(status) {
      const map = { 'pending': '待分配', 'in_progress': '分配中', 'completed': '已完成', 'cancelled': '已取消', 'failed': '失败' }
      return map[status] || status
    },
    getStrategyText(strategy) {
      const map = { 'FIFO': '先进先出', 'FEFO': '先过期先出', 'location_priority': '储位优先', 'batch_centralized': '批次集中' }
      return map[strategy] || strategy
    }
  }
}
</script>
